# Task
---
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: {{ project_class }}-task
  namespace: {{ kubernetes_pipeline_namespace }}
spec:
  resources:
    inputs:
    - name: workspace
      type: git
  params:
  - name: pathToYamlFile
    description: The path to the yaml file to deploy within the git source
    default: tekton-pipeline/appdeploy/01_js_nginx_dp.yaml
  - name: pathToContext
    description: The path to the build context, used by Kaniko - within the workspace
    default: .
  - name: pathToDockerfile
    description: The path to the dockerfile to build
    default: {{ dockerfile_name }}
  - name: imageUrl
    description: value should be like - us.icr.io/test_namespace/builtImageApp
    default: {{ image_url }}
    # default: 192.168.12.29:5000/k8s-tekton/k8sdocs
  - name: imageTag
    description: Tag to apply to the built image
    default: {{ image_tag }}
  steps:
  - name: list-k8sdocs-src
    image: docker-registry.21vcloud.com/tekton-controller/alpine:latest
    script: |
      ls -al $(inputs.resources.workspace.path)
  # - name: build-k8sdocs-image-and-push-to-registry
  #   #image: gcr.io/kaniko-project/executor
  #   #image: 10.100.2.125:5000/kaniko-project/executor:v1.3.0
  #   image: docker-registry.21vcloud.com/tekton-controller/kaniko-project-executor:latest
  #   command:
  #     - /kaniko/executor
  #   args:
  #     - "--dockerfile=$(inputs.params.pathToDockerfile)"
  #     - "--verbosity=debug"
  #     - "--skip-tls-verify"
  #     - "--destination=$(inputs.params.imageUrl):$(inputs.params.imageTag)"
  #     - "--context=$(inputs.resources.workspace.path)/$(inputs.params.pathToContext)/"
  #   volumeMounts:
  #     - name: harbor-login
  #       mountPath: /kaniko/.docker/
  #     # - name: kaniko-cache
  #     #   mountPath: /kaniko/cache
  - name: clone-app-manifests
    image: docker-registry.21vcloud.com/tekton-controller/alpine/git:v2.26.2
    script: |
      # git config
      git config --global user.email "you@example.com"
      git config --global user.name "Your Name"

      # clone k8s manifests
      git clone "{{ k8s_manifests_url }}"

      # enter root project
      cd "{{ k8s_manifests_name }}/{{ env }}/{{project_class}}/{{project_name}}"

      EXIT_CODE=`cat {{ project_name }}-dep.yaml | grep "image: $(inputs.params.imageUrl):$(inputs.params.imageTag)" | wc -l`
      if [ "$EXIT_CODE" -eq 0 ]
      then
        # list project
        ls -al

        # add new file
        #touch files
        # get image line count
        image_row=`cat -n {{ project_name }}-dep.yaml | grep "image:" | awk '{print $1}'`
        sed -i -e "${image_row}c \          image: $(inputs.params.imageUrl):$(inputs.params.imageTag)" {{ project_name }}-dep.yaml

        # git add file
        git add ./* -f

        # git commmit
        git commit -m "update manifests image tag to {{image_tag}}"

        # push code
        git push origin master
      else
        echo "app version is not change."
        exit 0
      fi
  - name: deploy-{{ project_name }}
    image: docker-registry.21vcloud.com/tekton-controller/k8s-kubectl:v1.20.2
    command: ["kubectl"]
    args:
      - "apply"
      - "-f"
      - "/tmp/"
    volumeMounts:
      - name: {{ project_name }}-argo
        mountPath: /tmp/
  - name: argo-sync-app-to-target-status
    image: docker-registry.21vcloud.com/tekton-controller/argoproj/argocd:v1.8.7
    script: |
      #/bin/bash
      set -x
      # login argo server
      argocd login {{ argo_server }} --username {{ argo_server_user }} --password {{ argo_server_user_pass }} --insecure
      # app list
      argocd app list
      # check app status code
      # sync app from git repository
      argocd app sync {{ project_name }}
      # normal exit process
      exit 0
  volumes:
    - name: harbor-login
      configMap:
        name: harbor-login
    - name: {{ project_name }}-argo
      configMap:
        name: {{ project_name }}-argo
    # - name: kaniko-cache
    #   persistentVolumeClaim:
    #     claimName: kaniko-cache-pvc
